package servlet;

import model.User;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserLoginServlet", urlPatterns = "/user_login")
public class UserLoginServlet extends HttpServlet {
    private UserService uService = new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//       接收请求 接收参数 ue password
        String ue= request.getParameter("ue");
        String password = request.getParameter("password");

//         调用业务层 Login => user
        User user = uService.login(ue, password);
//        给出响应
        if (user==null){
//            失败响应
            request.setAttribute("failMsg","用户名、邮箱或者密码错误，请重新登录!!!");
            request.getRequestDispatcher("user_login.jsp").forward(request,response);
        }else{
//            成功响应
            request.getSession().setAttribute("user",user);
            request.getRequestDispatcher("user_center.jsp").forward(request,response);
        }
//


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
